const Service = require('egg').Service;

class AwardService extends Service {
  async list(data) {
    const condition = {};
    condition.name = new RegExp(data.name);
    condition.deleted = false; // 未删除的
    if (data.type != '') {
      condition.type = data.type;
    }

    const awards = await this.app.model.Award.find(condition, {
      name: 1,
      type: 1,
      unit_price: 1,
      creator: 1,
      create_time: 1,
      total_inventory: 1,
      campaigns: 1,
    })
      .sort({
        create_time: -1,
      })
      .limit(parseInt(data.pageSize))
      .skip((data.pageIndex - 1) * data.pageSize);

    const total = await this.app.model.Award.count(condition);
    return {
      list: awards,
      total: total,
      pageIndex: data.pageIndex,
    };
  }

  async chooseList(data) {
    const condition = {};
    condition.name = new RegExp(data.name);
    condition.deleted = false; // 未删除的
    if (data.type != '') {
      condition.type = data.type;
    }

    condition.campaign = [];

    const list = await this.app.model.Award.find(condition);
    const total = await this.app.model.Award.count(condition);

    return {
      list: list,
      total: total,
    };
  }
}

module.exports = AwardService;
